import {
  repairList,
  repairDetail,
  repairAdd,
  repairEdit,
  repairInfoAdd,
  repairInfoEdit,
  repairInfoList,
  repairInfoDetail,
} from '@/services/repair';
import prompt from '@/components/Prompt';

export default {
  namespace: 'repair',

  state: {
    repairList: {},
    detail: {},
    repairInfoList: {},
    infoDetail: {},
  },

  effects: {
    *repairList({ payload, callback }, { call, put }) {
      const response = yield call(repairList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        callback && callback(list);
        yield put({
          type: 'list',
          payload: {
            repairList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
      }
    },

    *repairDetail({ payload, callback }, { call, put }) {
      const response = yield call(repairDetail, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            detail: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *repairInfoDetail({ payload }, { call, put }) {
      const response = yield call(repairInfoDetail, payload);
      if (response.code === 0) {
        yield put({
          type: 'infoDetailPut',
          payload: {
            infoDetail: response.data,
          },
        });
      }
    },
    *repairInfoList({ payload }, { call, put }) {
      const response = yield call(repairInfoList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'info',
          payload: {
            repairInfoList: {
              [payload.id]: {
                pagination: {
                  current: pageNum,
                  pageSize,
                  total,
                },
                list,
              },
            },
          },
        });
      }
    },
    *repairInfoOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? repairInfoEdit : repairInfoAdd, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        prompt({ content: message });
        if (callback) callback('success');
      }
    },
    *repairOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? repairEdit : repairAdd, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
  },

  reducers: {
    infoDetailPut(state, { payload }) {
      return {
        ...state,
        infoDetail: payload.infoDetail,
      };
    },
    list(state, { payload }) {
      return {
        ...state,
        repairList: payload.repairList,
      };
    },
    info(state, { payload }) {
      return {
        ...state,
        repairInfoList: { ...state.repairInfoList, ...payload.repairInfoList },
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        detail: { ...state.detail, ...payload.detail },
      };
    },
  },
};
